package com.sinothk.widget.loadingRecyclerView.demo.staggeredGridDemo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.sinothk.widget.loadingRecyclerView.demo.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2019/7/31.
 */

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.OneViewHolder> {
    private ArrayList<String> dataList = new ArrayList<>();

    public void replaceAll(ArrayList<String> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    /**
     * 插入数据使用notifyItemInserted，如果要使用插入动画，必须使用notifyItemInserted
     * 才会有效果。即便不需要使用插入动画，也建议使用notifyItemInserted方式添加数据，
     * 不然容易出现闪动和间距错乱的问题
     */
    public void addData(int position, ArrayList<String> list) {
        dataList.addAll(position, list);
        notifyItemInserted(position);
    }

    //移除数据使用notifyItemRemoved
    public void removeData(int position) {
        dataList.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public DemoAdapter.OneViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_water_fall, parent, false));
    }

    @Override
    public void onBindViewHolder(DemoAdapter.OneViewHolder holder, int position) {
        holder.setData(dataList.get(position), position);
    }


    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

//    public class BaseViewHolder extends RecyclerView.ViewHolder {
//
//        public BaseViewHolder(View itemView) {
//            super(itemView);
//        }
//
//        void setData(Object data, int position) {
//
//        }
//    }

    protected static class OneViewHolder extends RecyclerView.ViewHolder {

        private final ImageView ivImage;

        public OneViewHolder(View view) {
            super(view);
            ivImage = (ImageView) view.findViewById(R.id.iv_item_water_fall);
        }

        void setData(Object data, int position) {
            if (data != null) {
                ivImage.setImageResource(R.drawable.headimg);
                //需要Item高度不同才能出现瀑布流的效果，此处简单粗暴地设置一下高度
                if (position % 2 == 0) {
                    ivImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 600));
                } else {
                    ivImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 420));
                }
            }
        }
    }


}
